import json

from django.db.models import F
from django.forms import model_to_dict
from django.http import JsonResponse
from django.views import View
from django.shortcuts import render, HttpResponse
from django.db import DatabaseError

from student.models import StudentInfo
from sc.models import SCInfo
from course.models import ClassInfo


class addStudent(View):
    """添加学生"""
    def get(self, request):
        return HttpResponse('this is the adding page.....')

    def post(self, request):
        # 获取参数
        Ssex = request.POST.get('Ssex')
        Sname = request.POST.get('Sname')
        Sage = request.POST.get('Sage')
        Sdept = request.POST.get('Sdept')
        Sno = request.POST.get('Sno')
        # print(Sno, Ssex, Sname, Sdept, Sage)

        # 数据库操作
        try:
            StudentInfo.objects.create(Sno=Sno, Sname=Sname, Sdept=Sdept, Sage=Sage, Ssex=Ssex)
            data = StudentInfo.objects.filter(Sno=Sno).values()
            data = data[0]

            # print(data)

        except DatabaseError:
            return JsonResponse({'code': 0, 'msg': "添加失败"})
        # 返回结果
        return JsonResponse({'code': 200, 'msg': "修改成功", 'data': data})

class delStuent(View):
    """删除学生的数据"""
    def get(self, request):
        return HttpResponse("this is the del function!")

    def post(self, request):
        # 获取参数
        Sno = request.POST.get("Sno")
        # 删除数据
        try:
            # 对选课表里的数据进行删除
            data2 = list(SCInfo.objects.filter(Sno=Sno).values())
            for i in data2:
                # 退课以后课程容量加1
                ClassInfo.objects.filter(Cno=i['Cno_id']).update(Ccredit=F('Ccredit') + 1)
                # 删除记录
                SCInfo.objects.filter(Sno=Sno).first().delete()
            # 删除对应的学生
            StudentInfo.objects.filter(Sno=Sno).delete()
        except DatabaseError:
            return JsonResponse({'code': 0, 'msg': '操作失败'})

        # 返回参数
        return JsonResponse({'code': 200, 'msg': '删除成功'})


class edit(View):
    """
    修改信息
    """
    def get(self, request):
        return HttpResponse("this is 修改信息 request")

    def post(self, request):
        # 接收参数
        Ssex = request.POST.get('Ssex')
        Sname = request.POST.get('Sname')
        Sage = request.POST.get('Sage')
        Sdept = request.POST.get('Sdept')
        Sno = request.POST.get('Sno')
        print(Sno, Ssex, Sname, Sdept, Sage)

        # 修改数据库
        try:
            StudentInfo.objects.filter(Sno=Sno).update(Sname=Sname, Sdept=Sdept, Sage=Sage, Ssex=Ssex)
            data = StudentInfo.objects.filter(Sno=Sno).values()
            data = data[0]

        except DatabaseError:
            return JsonResponse({'code': 0, 'msg': "注册失败"})

        # 返回信息
        return JsonResponse({'code': 200, 'msg': "修改成功", 'data': data})


class index(View):

    # 主要的逻辑代码
    def get(self, request):
        # 修改数据
        # StudentInfo.objects.create(Sno='95001', Sname='张三', Ssex='男', Sage='20', Sdept='CS')
        # StudentInfo.objects.create(Sno='95002', Sname='李四', Ssex='男', Sage='21', Sdept='IS')
        # StudentInfo.objects.create(Sno='95003', Sname='王五', Ssex='男', Sage='18', Sdept='MA')
        # StudentInfo.objects.create(Sno='95004', Sname='马六', Ssex='女', Sage='19', Sdept='CS')
        # StudentInfo.objects.create(Sno='95005', Sname='苏三', Ssex='女', Sage='19', Sdept='IS')
        # StudentInfo.objects.create(Sno='95006', Sname='刘七', Ssex='女', Sage='18', Sdept='IS')
        # StudentInfo.objects.create(Sno='95007', Sname='刘三姐', Ssex='女', Sage='22', Sdept='IS')
        # StudentInfo.objects.create(Sno='95008', Sname='欧阳锋', Ssex='男', Sage='23', Sdept='MS')
        # StudentInfo.objects.create(Sno='95009', Sname='欧阳大侠', Ssex='男', Sage='22', Sdept='MS')

        data = StudentInfo.objects.all().order_by('Sno')
        dic = {}
        Lst = []
        for i in data:
            Lst.append(model_to_dict(i))
        dic['code'] = '1'  # 成功
        dic['message'] = '获取成功'  # 成功
        dic['data'] = Lst
        # print(type(HttpResponse(json.dumps(dic, ensure_ascii=False))))
        # return HttpResponse(json.dumps(dic, ensure_ascii=False))
        return JsonResponse(dic)

